package com.hopu.oa.controller.UserManage;

import com.hopu.oa.entity.user.AoaDept;
import com.hopu.oa.entity.user.AoaPosition;
import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.UserManageService.DeptManageService;
import com.hopu.oa.until.data.DateUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DeptManageController {
    @Autowired
    private DeptManageService deptService;

    @RequestMapping("deptmanage")
    public String findAllDept(String baseKey, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "4") int size, Model model) {

        //总数据
        int totalElements = deptService.findAllDept(baseKey, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaDept> deptList = deptService.findAllDept(baseKey, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("depts", deptList);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("url", "sedeptmanage");

        return "user/deptmanage";
    }

    @RequestMapping("sedeptmanage")
    public String sedeptmanage(String baseKey, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "size", defaultValue = "4") int size, Model model) {

        //总数据
        int totalElements = deptService.findAllDept(baseKey, page, 0).size();
        //有几页
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaDept> deptList = deptService.findAllDept(baseKey, (page - 1) * size, size);
        HashMap<String, Integer> map = new HashMap<>();
        map.put("totalElements", totalElements);
        map.put("totalPages", totalPages);
        map.put("number", page);
        map.put("size", size);
        model.addAttribute("page", map);
        model.addAttribute("depts", deptList);
        model.addAttribute("baseKey", baseKey);
        model.addAttribute("url", "sedeptmanage");
        return "user/deptmanagetable";
    }


    @RequestMapping("deptedit")
    public String deptedit(Integer dept, Model model) {
        if (dept == null) {
            return "user/deptedit";
        } else {
            AoaDept dept1 = deptService.findDeptById(dept);
            model.addAttribute("dept", dept1);
            return "user/deptedit";
        }
    }

    @RequestMapping("modifdept")
    public String modifdept(@Param("deptName") String deptName,
                            @Param("deptTel") String deptTel,
                            @Param("deptFax") String deptFax,
                            @Param("email") String email,
                            @Param("deptAddr") String deptAddr, AoaDept dept, Model model) {
        if (dept.getDeptId() != null) {
            System.out.println("进来了");
            AoaDept deptByDeptName = deptService.findDeptByDeptName(deptName);
            AoaDept deptById = deptService.findDeptById(dept.getDeptId());

            System.out.println(deptByDeptName);
            if (deptById.getDeptName().equals(deptName) && deptService.findDeptByDeptNameId(deptName) > 1) {
                System.out.println("你好");
                deptService.updateDept(deptName, deptTel, deptFax, email, deptAddr, DateUtils.getDateTimeString(), dept.getDeptId());
                return "redirect:deptmanage";
            } else {
                if (deptByDeptName == null) {
                    System.out.println("修改");
                    deptService.updateDept(deptName, deptTel, deptFax, email, deptAddr, DateUtils.getDateTimeString(), dept.getDeptId());
                    return "redirect:deptmanage";
                } else {
                    model.addAttribute("errormess", "添加重复名字");
                    return "user/deptedit";
                }
            }
        } else {
            System.out.println("你好啊");
            AoaDept deptByDeptName = deptService.findDeptByDeptName(deptName);
            System.out.println(deptByDeptName);
            if (deptByDeptName == null) {
                System.out.println("增加");
                deptService.insertDept(deptName, deptTel, deptFax, email, deptAddr, DateUtils.getDateTimeString(), DateUtils.getDateTimeString());
                return "redirect:deptmanage";
            } else {
                model.addAttribute("errormess", "添加重复名字");
                return "user/deptedit";
            }
        }
    }

    @RequestMapping("deleteDept")
    public String deleteDept(int deptid, Model model) {
        List<AoaUser> allUserByDeptId = deptService.findAllUserByDeptId(deptid);
        if (allUserByDeptId.size() == 0) {
            System.out.println("#######################");
            deptService.deleteDept(deptid);
            return "redirect:deptmanage";
        } else {
            model.addAttribute("errormess", "该角色还有员工！");
            return "redirect:deptmanage";
        }
    }

    /**
     * 人事调动
     *
     * @param deptid
     * @param model
     * @return
     */
    @RequestMapping("readdept")
    public String readdept(int deptid, Model model) {
        List<AoaUser> users = deptService.selectUserBydeptId(deptid);
        AoaUser relaName = deptService.selectRelaName(deptid);
        AoaDept dept = deptService.selectDeptById(deptid);
        model.addAttribute("dept", dept);
        model.addAttribute("depts", deptService.selectdeptAll());
        model.addAttribute("deptmanage", relaName);
        model.addAttribute("positions", deptService.selectPositionById(deptid));
        model.addAttribute("deptuser", users);
        return "user/deptread";
    }

    @RequestMapping("changePosition")
    @ResponseBody
    public Map<String, Object> changePosition(int deptid) {
        HashMap<String, Object> map = new HashMap<>();
        List<AoaPosition> positions = deptService.selectPositionById(deptid);
        map.put("positions", positions);
        return map;
    }

    @RequestMapping("deptandpositionchange")
    public String updateposition(@Param("userid") int userid, @Param("changedeptid") int changedeptid, @Param("positionid") int positionid) {
        deptService.updatePositionById(userid, positionid, changedeptid);
        return "redirect:deptmanage";
    }

    @RequestMapping("deptmanagerchange")
    public String deptmanagerchange(@Param("oldmanageid") Integer oldmanageid, @Param("changedeptid2") Integer changedeptid2, @Param("positionid") Integer positionid,
                                    @Param("newmanageid") int newmanageid, @Param("deptid") Integer deptid, Model model) {
        if (changedeptid2 != null && positionid != null) {
            deptService.updatePositionById(oldmanageid, positionid, changedeptid2);
        }
        deptService.updateDeptmanager(newmanageid, deptid);
        model.addAttribute("deptid", changedeptid2);
        return "forward:readdept";
    }


}
